package cn.livingCloud.service.impl;

import cn.livingCloud.dto.*;
import cn.livingCloud.entity.content.ReplyRule;
import cn.livingCloud.repository.ReplyRuleMapper;
import cn.livingCloud.repository.TextReplyContentMapper;
import cn.livingCloud.service.ReplyRuleService;
import cn.livingCloud.service.impl.operation.ArticlesReplyRuleFormDtoUpdater;
import cn.livingCloud.service.impl.operation.TextReplyRuleFormDtoUpdater;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sujiantang on 2015/12/9.
 */
@Service
public class ReplyRuleServiceImpl implements ReplyRuleService {
    @Autowired
    private ReplyRuleMapper ruleMapper;

    @Autowired
    private TextReplyContentMapper textMapper;

    @Override
    public void findReplyRuleList(ReplyRuleListDto formDto) {
        ruleMapper.findReplyRuleList(formDto);
        formDto.addAllList(ruleMapper.findReplyRuleList(formDto));
    }

    @Override
    public ReplyRuleDto updateState(String uuid, boolean state) {
        ReplyRule replyRule = ruleMapper.findReplyRule(uuid);

        //如果前台传入执行的状态和数据库的状态相同那么就抛出异常
        if (state == replyRule.isEnable()) {
            throw new IllegalStateException("禁用/启用失败,请检查账号状态是否被禁用/启用!");
        }
        //如果前台的状态和数据库的状态不相同那么就执行当前传入的操作
        replyRule.setEnable(state);
        ruleMapper.updateEnable(replyRule);
        return new ReplyRuleDto(replyRule);
    }

    @Override
    public void editOrSave(TextReplyRuleFormDto formDto) {
        TextReplyRuleFormDtoUpdater updater = new TextReplyRuleFormDtoUpdater(formDto);
        updater.update();
    }

    @Override
    public void delete(String uuid) {
        ReplyRule replyRule = ruleMapper.findReplyRule(uuid);
        replyRule.delete();
    }

    @Override
    public void updateArticlesReplyRule(ArticlesReplyRuleFormDto formDto) {
        ArticlesReplyRuleFormDtoUpdater updater = new ArticlesReplyRuleFormDtoUpdater(formDto);
        updater.update();
    }

    @Override
    public TextReplyRuleDto getTextReplyContent(String uuid) {
        ReplyRule replyRule = ruleMapper.findReplyRule(uuid);
        return new TextReplyRuleDto(replyRule);
    }

    @Override
    public TextReplyRuleFormDto loadTextReplyRuleFormDto(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return new TextReplyRuleFormDto();
        }
        ReplyRule replyRule = ruleMapper.findReplyRule(uuid);
        return new TextReplyRuleFormDto(replyRule);
    }

    @Override
    public ArticlesReplyRuleDto loadArticlesReplyRuleDto(String uuid) {
        ReplyRule replyRule = ruleMapper.findReplyRule(uuid);
        return new ArticlesReplyRuleDto(replyRule);
    }

    @Override
    public ArticlesReplyRuleFormDto loadArticlesReplyRuleFormDto(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return new ArticlesReplyRuleFormDto();
        }
        ReplyRule replyRule = ruleMapper.findReplyRule(uuid);
        return new ArticlesReplyRuleFormDto(replyRule);
    }

    @Override
    public List<ReplyRuleDto> searchRuleList(String key) {
        List<ReplyRuleDto> ruleDtos = new ArrayList<>();
        List<ReplyRule> replyRules = ruleMapper.searchRule(key);
        for (ReplyRule replyRule : replyRules) {
            ruleDtos.add(new ReplyRuleDto(replyRule));
        }
        return ruleDtos;
    }
}
